package com.hncboy.chatgpt.front.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hncboy.chatgpt.base.domain.entity.ChatCustomerDO;
import com.hncboy.chatgpt.base.domain.entity.ChatOrderDO;
import com.hncboy.chatgpt.base.mapper.ChatCustomerMapper;
import com.hncboy.chatgpt.front.domain.vo.ChatCustomerVO;
import com.hncboy.chatgpt.front.service.ChatOrderService;
import com.hncboy.chatgpt.front.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/****************************************
 * @name com.hncboy.chatgpt.front.service.impl
 * @description
 * @author limi
 * @since 2023-04-28 17:33
 ***************************************/
@Slf4j
@Service
public class CustomerServiceImpl
        extends ServiceImpl<ChatCustomerMapper, ChatCustomerDO>
        implements CustomerService {

    @Value("${chat.limits}")
    Integer limits;
    @Resource
    private ChatOrderService chatOrderService;

    @Override
    public ChatCustomerVO initCustomer(Long id) {
        ChatCustomerVO chatCustomerVO = new ChatCustomerVO();
        ChatCustomerDO chatCustomerDO = super.getById(id);
        BeanUtil.copyProperties(chatCustomerDO, chatCustomerVO);
        QueryWrapper<ChatOrderDO> queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(new ChatOrderDO() {{
            setCustomerId(id);
        }});
        List<ChatOrderDO> list = chatOrderService.list(queryWrapper);
        chatCustomerVO.setOrderInfos(list);
        return chatCustomerVO;
    }

    @Override
    public ChatCustomerDO getCustomer(String email) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(new ChatCustomerDO() {{
            setEmail(email);
        }});
        return super.getOne(queryWrapper);
    }

    @Override
    public ChatCustomerDO getCustomer(Long id) {
        return super.getById(id);
    }

    @Override
    public boolean saveOrUpdate(ChatCustomerDO chatCustomerDO) {
        chatCustomerDO.setLimits(limits);
        chatCustomerDO.setStatus(0);
        chatCustomerDO.setLimits(limits);
        chatCustomerDO.setNickName("Mi Future Ais");
        return super.saveOrUpdate(chatCustomerDO);
    }
}
